package com.codeplatform.mapper;

import com.codeplatform.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户数据访问接口
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
    
    /**
     * 根据用户名查找用户
     */
    @Select("SELECT * FROM users WHERE username = #{username}")
    User findByUsername(@Param("username") String username);
    
    /**
     * 根据邮箱查找用户
     */
    @Select("SELECT * FROM users WHERE email = #{email}")
    User findByEmail(@Param("email") String email);
    
    /**
     * 根据用户名或邮箱查找用户
     */
    @Select("SELECT * FROM users WHERE username = #{usernameOrEmail} OR email = #{usernameOrEmail}")
    User findByUsernameOrEmail(@Param("usernameOrEmail") String usernameOrEmail);
    
    /**
     * 检查用户名是否存在
     */
    @Select("SELECT COUNT(*) FROM users WHERE username = #{username}")
    int existsByUsername(@Param("username") String username);
    
    /**
     * 检查邮箱是否存在
     */
    @Select("SELECT COUNT(*) FROM users WHERE email = #{email}")
    int existsByEmail(@Param("email") String email);
    
    /**
     * 根据状态查找用户
     */
    @Select("SELECT * FROM users WHERE status = #{status}")
    List<User> findByStatus(@Param("status") Integer status);
    
    /**
     * 查找活跃用户
     */
    @Select("SELECT * FROM users WHERE status = 1")
    List<User> findActiveUsers();
    
    /**
     * 根据角色查找用户
     */
    @Select("SELECT * FROM users WHERE role = #{role}")
    List<User> findByRole(@Param("role") String role);
    
    /**
     * 查找管理员用户
     */
    @Select("SELECT * FROM users WHERE role = 'admin'")
    List<User> findAdminUsers();
    
    /**
     * 根据邮箱验证状态查找用户
     */
    @Select("SELECT * FROM users WHERE email_verified = #{emailVerified}")
    List<User> findByEmailVerified(@Param("emailVerified") Boolean emailVerified);
    
    /**
     * 查找最近注册的用户
     */
    @Select("SELECT * FROM users WHERE created_at >= #{since} ORDER BY created_at DESC")
    List<User> findRecentUsers(@Param("since") LocalDateTime since);
    
    /**
     * 查找最近登录的用户
     */
    @Select("SELECT * FROM users WHERE last_login_at >= #{since} ORDER BY last_login_at DESC")
    List<User> findRecentlyLoggedInUsers(@Param("since") LocalDateTime since);
    
    /**
     * 统计用户总数
     */
    @Select("SELECT COUNT(*) FROM users")
    long countTotalUsers();
    
    /**
     * 统计活跃用户数
     */
    @Select("SELECT COUNT(*) FROM users WHERE status = 1")
    long countActiveUsers();
    
    /**
     * 统计今日新增用户数
     */
    @Select("SELECT COUNT(*) FROM users WHERE DATE(created_at) = CURDATE()")
    long countTodayNewUsers();
    
    /**
     * 统计本月新增用户数
     */
    @Select("SELECT COUNT(*) FROM users WHERE YEAR(created_at) = YEAR(NOW()) AND MONTH(created_at) = MONTH(NOW())")
    long countThisMonthNewUsers();
    
    /**
     * 根据关键词搜索用户
     */
    @Select("SELECT * FROM users WHERE username LIKE CONCAT('%', #{keyword}, '%') OR nickname LIKE CONCAT('%', #{keyword}, '%') OR email LIKE CONCAT('%', #{keyword}, '%')")
    List<User> searchUsers(@Param("keyword") String keyword);
    
    /**
     * 查找长时间未登录的用户
     */
    @Select("SELECT * FROM users WHERE last_login_at < #{threshold} OR last_login_at IS NULL")
    List<User> findInactiveUsers(@Param("threshold") LocalDateTime threshold);
    
    /**
     * 更新用户最后登录信息
     */
    @Update("UPDATE users SET last_login_at = #{loginTime}, last_login_ip = #{ipAddress}, login_count = login_count + 1 WHERE id = #{userId}")
    void updateLastLoginInfo(@Param("userId") Long userId, @Param("loginTime") LocalDateTime loginTime, @Param("ipAddress") String ipAddress);
}